package model;

import gvjava.org.json.JSONException;
import gvjava.org.json.JSONObject;

import java.util.HashMap;

import com.tecnick.htmlutils.htmlentities.HTMLEntities;


import language.LangIntrf;



public class HistoryLog {
	
	private static HistoryLog inst = new HistoryLog();
	private String CREATE_BUSINESS_ACCOUNT_TYPE = "createBusinessAccount";
	private String UPDATE_BUSINESS_ACCOUNT_TYPE = "updateBusinessAccount";
	private String CREATE_ORDER_TYPE = "createOrder";
	private String CLOSE_ORDER_ACCOUNT_TYPE = "closeOrder"; //missing
	private String SEND_MESSAGE_TYPE = "sendMessage";
	private String CHANGE_ORDER_STATUS_TYPE = "changeOrderStatus";
	
	private HashMap<String, RecordType> recordTypeHash;
	
	private abstract class RecordType
	{
		public String getRecord(JSONObject json, LangIntrf lang)
		{
			
			try {
				return getRestRecord(json,lang);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "";
		}
		
		protected abstract String getRestRecord(JSONObject json, LangIntrf lang) throws JSONException;
		
	}
	
	public static HistoryLog getInst()
	{
		return inst;
	}
		
	
	private HistoryLog() {
		
		recordTypeHash = new HashMap<String, HistoryLog.RecordType>();
		
		recordTypeHash.put(CREATE_BUSINESS_ACCOUNT_TYPE, new RecordType() {
			@Override
			protected String getRestRecord(JSONObject json, LangIntrf lang) {
				return HTMLEntities.htmlentities(lang.getCREATE_BUSINESS_LOG());
			}
		});
		
		
		recordTypeHash.put(UPDATE_BUSINESS_ACCOUNT_TYPE, new RecordType() {
			@Override
			protected String getRestRecord(JSONObject json, LangIntrf lang) {
				return HTMLEntities.htmlentities(lang.getUPDATE_BUSINESS_LOG());
			}
		});
		
		
		recordTypeHash.put(CREATE_ORDER_TYPE, new RecordType() {			
			@Override
			protected String getRestRecord(JSONObject json, LangIntrf lang) throws JSONException {
				return HTMLEntities.htmlentities(lang.getCREATE_ORDER_1_LOG()+ json.getString("orderNum")+lang.getCREATE_ORDER_2_LOG());
					
			}
		});
		
		
		recordTypeHash.put(CLOSE_ORDER_ACCOUNT_TYPE, new RecordType() {			
			@Override
			protected String getRestRecord(JSONObject json, LangIntrf lang) throws JSONException {
				return HTMLEntities.htmlentities(lang.getCLOSE_ORDER_1_LOG()+ json.getString("orderNum")+lang.getCLOSE_ORDER_2_LOG());
			}
		});
		
		
		recordTypeHash.put(SEND_MESSAGE_TYPE, new RecordType() {			
			@Override
			protected String getRestRecord(JSONObject json, LangIntrf lang) throws JSONException {
				return HTMLEntities.htmlentities(lang.getSEND_MESSAGE_1_LOG()+ json.getString("orderNum")+lang.getSEND_MESSAGE_2_LOG());
			}
		});
		
		
		recordTypeHash.put(CHANGE_ORDER_STATUS_TYPE, new RecordType() {			
			@Override
			protected String getRestRecord(JSONObject json, LangIntrf lang) throws JSONException {
				return HTMLEntities.htmlentities(lang.getCHANGE_ORDER_STATUS_1_LOG()+ json.getString("orderNum")+lang.getCHANGE_ORDER_STATUS_2_LOG())+json.getString("from") + HTMLEntities.htmlentities(lang.getCHANGE_ORDER_STATUS_3_LOG())+json.getString("to");					
			}
		});
		

	}


	public String getCREATE_BUSINESS_ACCOUNT_TYPE() {
		return CREATE_BUSINESS_ACCOUNT_TYPE;
	}


	public String getUPDATE_BUSINESS_ACCOUNT_TYPE() {
		return UPDATE_BUSINESS_ACCOUNT_TYPE;
	}


	public String getCREATE_ORDER_TYPE() {
		return CREATE_ORDER_TYPE;
	}


	public String getCLOSE_ORDER_ACCOUNT_TYPE() {
		return CLOSE_ORDER_ACCOUNT_TYPE;
	}


	public String getSEND_MESSAGE_TYPE() {
		return SEND_MESSAGE_TYPE;
	}


	public String getCHANGE_ORDER_STATUS_TYPE() {
		return CHANGE_ORDER_STATUS_TYPE;
	}
	

	
	
	public String getHistoryLogRecord(String jsonRecord, LangIntrf lang,boolean isDate)
	{
		
		JSONObject json;
		try {
			json = new JSONObject(jsonRecord);
			
			if (isDate)
				return json.getString("date");
			
			String type = json.getString("type");
			RecordType recordType = recordTypeHash.get(type);
			
			if (recordType==null)
				return null;
			
			return recordType.getRecord(json, lang);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	
	

}
